package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipRepairRepositoryRepairDoc;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipRepairRepositoryDao;
import cc.comac.mom.dao.entity.EquipRepairRepositoryRepairDocDao;
import cc.comac.mom.datahandlers.base.DataHandler;
import cc.comac.mom.entity.EquipRepairRepositoryEntity;
import cc.comac.mom.entity.EquipRepairRepositoryRepairDocEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipRepairRepositoryRepairDocDataHandler 
			extends ServiceImpl<EquipRepairRepositoryRepairDocDao,EquipRepairRepositoryRepairDocEntity>
			implements DataHandler<EquipRepairRepositoryRepairDoc>{
	
	@Autowired
	private EquipRepairRepositoryRepairDocDao equipRepairRepositoryRepairDocDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private EquipRepairRepositoryDao equipRepairRepositoryDao;
	
	@Override
	public ResultBean save(EquipRepairRepositoryRepairDoc obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositoryRepairDocEntity entity = obj.getInnerData();
		
		long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
		String documentName = entity.getDocumentName();
		
		QueryWrapper<EquipRepairRepositoryRepairDocEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryRepairDocEntity>();
		queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
		queryWrapper.eq("document_name", documentName);
		queryWrapper.eq("row_status", 1);
		List<EquipRepairRepositoryRepairDocEntity> existsEntity = equipRepairRepositoryRepairDocDao.selectList(queryWrapper);
		
		if(existsEntity!=null && existsEntity.size()>0) {
			resultBean.setErrorMessge("equip.repair.repository.exists.repairDoc");
			resultBean.setMsgParams(new String[] {documentName});
			return resultBean;
		}
		entity.setDocumentUploadUser(baseEntityDataUtil.getCurrentUser());
		entity.setDocumentUploadUserJobNumber(baseEntityDataUtil.getJobNumber());
		baseEntityDataUtil.setBaseEntityData(entity);
		boolean save = this.save(entity);
		if(!save) {
			throw new RuntimeException("保存失败");
		}
		
		resultBean.setSuccess(true);
		return resultBean;
		
	}

	@Override
	public ResultBean update(EquipRepairRepositoryRepairDoc obj) {
		ResultBean resultBean=new ResultBean(false);
		resultBean.setMessage("equip.repair.repository.notSupportEdit");
		return resultBean;
	}

	@Override
	public ResultBean delete(EquipRepairRepositoryRepairDoc obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositoryRepairDocEntity entity = obj.getInnerData();
		
		long id = entity.getId();
		EquipRepairRepositoryRepairDocEntity existsEntity = equipRepairRepositoryRepairDocDao.selectById(id);
		if(existsEntity==null) {
			resultBean.setErrorMessge("equip.repair.repository.notExists");
			resultBean.setMsgParams(new String[] {id+""});
			return resultBean;
		}
		baseEntityDataUtil.updateBaseEntityData(entity);
		entity.setRowStatus(0);
		boolean save = this.updateById(entity);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			resultBean.setMsgParams(new String[] {entity.getDocumentName()});
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean save(List<EquipRepairRepositoryRepairDoc> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositoryRepairDocEntity> entityList = new ArrayList<>();
		
		for(EquipRepairRepositoryRepairDoc obj:objs) {
			EquipRepairRepositoryRepairDocEntity entity = obj.getInnerData();
			
			long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
			String documentName = entity.getDocumentName();
			
			QueryWrapper<EquipRepairRepositoryRepairDocEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryRepairDocEntity>();
			queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
			queryWrapper.eq("document_name", documentName);
			queryWrapper.eq("row_status", 1);
			List<EquipRepairRepositoryRepairDocEntity> existsEntity = equipRepairRepositoryRepairDocDao.selectList(queryWrapper);
			
			if(existsEntity!=null && existsEntity.size()>0) {
				resultBean.setErrorMessge("equip.repair.repository.exists.repairDoc");
				resultBean.setMsgParams(new String[] {documentName});
				return resultBean;
			}
			entity.setDocumentUploadUser(baseEntityDataUtil.getCurrentUser());
			entity.setDocumentUploadUserJobNumber(baseEntityDataUtil.getJobNumber());
			baseEntityDataUtil.setBaseEntityData(entity);
			entityList.add(entity);
		}
		
		
		boolean save = this.saveBatch(entityList);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean update(List<EquipRepairRepositoryRepairDoc> objs) {
		ResultBean resultBean=new ResultBean(false);
		resultBean.setMessage("equip.repair.repository.notSupportEdit");
		return resultBean;
	}

	@Override
	public ResultBean delete(List<EquipRepairRepositoryRepairDoc> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositoryRepairDocEntity> entityList = new ArrayList<>();
		for(EquipRepairRepositoryRepairDoc obj:objs) {
			EquipRepairRepositoryRepairDocEntity entity = obj.getInnerData();
			long id = entity.getId();
			EquipRepairRepositoryRepairDocEntity existsEntity = equipRepairRepositoryRepairDocDao.selectById(id);
			if(existsEntity==null) {
				resultBean.setErrorMessge("equip.repair.repository.notExists");
				resultBean.setMsgParams(new String[] {entity.getDocumentName()});
				return resultBean;
			}
			baseEntityDataUtil.updateBaseEntityData(entity);
			entity.setRowStatus(0);
			entityList.add(entity);
		}
		
		boolean save = this.updateBatchById(entityList);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public EquipRepairRepositoryRepairDoc getById(long id) {
		EquipRepairRepositoryRepairDocEntity entity = equipRepairRepositoryRepairDocDao.selectById(id);
		if(entity!=null) {
			EquipRepairRepositoryRepairDoc bean = new EquipRepairRepositoryRepairDoc();
			bean.setInnerData(entity);
			return bean;
		}
		return null;
	}

	public ResultDev selectByCondition(EquipRepairRepositoryRepairDoc obj) {
		ResultDev resultDev = new ResultDev();
    	QueryWrapper<EquipRepairRepositoryEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("equip_category_id",obj.getEquipCategoryId());
    	queryWrapper.eq("fault_cause_id",obj.getFaultCauseId());
    	
    	List<EquipRepairRepositoryEntity> listEquipRepairRepositoryEntity = equipRepairRepositoryDao.selectList(queryWrapper);

    	if(listEquipRepairRepositoryEntity == null || listEquipRepairRepositoryEntity.size()<1) {
    		return null;
    	}
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		QueryWrapper<EquipRepairRepositoryRepairDocEntity> queryWrapper1 = new QueryWrapper<EquipRepairRepositoryRepairDocEntity>();
		queryWrapper1.eq("row_status", 1);
		queryWrapper1.eq("equip_repair_repository_id", listEquipRepairRepositoryEntity.get(0).getId());
		queryWrapper1.orderByDesc("creation_time");
		Page<EquipRepairRepositoryRepairDocEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipRepairRepositoryRepairDocEntity> iPage = equipRepairRepositoryRepairDocDao.selectPage(page, queryWrapper1);
		List<EquipRepairRepositoryRepairDocEntity> list = iPage.getRecords();
		resultDev.setEntityList(list);
		resultDev.setPageCount((int)iPage.getPages());
		resultDev.setTotalQuantity((int)iPage.getTotal());
		return resultDev;
	}
}
